package io.metaxk.module.mes.controller.admin.md;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy;
import io.metaxk.framework.common.pojo.PageResult;
import io.metaxk.module.mes.controller.admin.md.vo.VendorExcelVo;
import io.metaxk.module.mes.controller.admin.md.vo.VendorQueryVo;
import io.metaxk.module.mes.dal.dataobject.md.Vendor;
import io.metaxk.module.mes.service.md.VendorService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import javax.servlet.http.*;
import java.net.URLEncoder;
import java.util.*;
import java.io.IOException;
import io.metaxk.framework.common.pojo.CommonResult;
import static io.metaxk.framework.common.exception.util.ServiceExceptionUtil.exception;
import static io.metaxk.framework.common.pojo.CommonResult.success;
import static io.metaxk.module.mes.enums.ErrorCodeConstants.*;



/**
 * @author 万界星空
 * @time 2023/6/20 15:33
 */
@Tag(name = "管理后台 - 供应商")
@RestController
@RequestMapping("/mes/md/vendor")
public class VendorController {



    @Resource
    private VendorService vendorService;



    @GetMapping("/list")
    @Operation(summary = "供应商条件分页查询")
    @PreAuthorize("@ss.hasPermission('md:vendor:list')")
    public CommonResult<PageResult<Vendor>> getVendorPage( VendorQueryVo pageVO) {
        PageResult<Vendor> pageResult = vendorService.getVendorPage(pageVO);
        return success(pageResult);
    }



    @PostMapping("/save")
    @Operation(summary = "新增供应商")
    @PreAuthorize("@ss.hasPermission('md:vendor:save')")
    public CommonResult<Integer> save( @RequestBody Vendor vendor) {
        if( vendorService.findVendorByCode(vendor.getVendorCode()) != null){
            throw exception(VENDOR_CODE_EXIST);
        }
        if( vendorService.findVendorByName(vendor.getVendorName()) != null){
            throw exception(VENDOR_NAME_EXIST);
        }
        return success(vendorService.insertVendor(vendor)).setMsg("新增成功");
    }




    @DeleteMapping("/batch")
    @Operation(summary = "删除供应商")
    @PreAuthorize("@ss.hasPermission('md:vendor:delete')")
    public CommonResult<Integer>  batch(@RequestBody List<Long>  ids) {
        return success( vendorService.removeVendor(ids)).setMsg("删除成功");
    }




    @PutMapping("/update")
    @Operation(summary = "修改供应商")
    @PreAuthorize("@ss.hasPermission('md:vendor:update')")
    public CommonResult<Integer> update( @RequestBody Vendor vendor) {
        return success(vendorService.updateVendor(vendor)).setMsg("修改成功");
    }


    @GetMapping("/get/{id}")
    @Operation(summary = "供应商详情")
    @PreAuthorize("@ss.hasPermission('md:vendor:get')")
    public CommonResult<Vendor>  getVendorById(@PathVariable Long id) {
        return success( vendorService.getVendor(id));
    }






    @GetMapping("/export")
    @Operation(summary = "供应商导出")
    @PreAuthorize("@ss.hasPermission('md:vendor:export')")
    public void export(HttpServletResponse response){
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("供应商管理", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
            SimpleColumnWidthStyleStrategy styleStrategy = new SimpleColumnWidthStyleStrategy(20);
            EasyExcel.write(response.getOutputStream(), VendorExcelVo.class).registerWriteHandler(styleStrategy).sheet("供应商管理").doWrite(vendorService.listData());
        } catch (IOException e) {
            throw exception(EXPORT_DATA_ERROR);
        }
    }


    @GetMapping("/listAll")
    @Operation(summary = "供应商条件分页查询")
    public CommonResult<List<Vendor>> listAll() {
        List<Vendor> list = vendorService.listAll();
        return success(list);
    }
}
